package org.msfj.admin.org.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import org.msfj.admin.model.Role;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.model.UserRole;
import org.msfj.admin.org.service.RoleService;
import org.msfj.admin.vo.RoleVo;
import org.msfj.admin.model.Role;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.model.UserRole;
import org.msfj.admin.org.service.RoleService;
import org.msfj.admin.vo.RoleVo;
import org.msfj.admin.model.Role;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.model.UserRole;
import org.msfj.admin.vo.RoleVo;
import org.springframework.stereotype.Service;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.msfj.admin.org.mapper.RoleMenuMapper;
import org.msfj.admin.org.mapper.UserRoleMapper;
import org.msfj.admin.org.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 系统角色信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "role")
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {


    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 根据条件分页查询系统角色信息列表
     *
     * @param roleVo
     * @param page 当前第几页
     * @param rows 每页多少条记录
     * @return PageInfo<Role>
     */
    @Override
    //@Cacheable
    public PageInfo<RoleVo> getByRole(RoleVo roleVo, int page, int rows){
        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (roleVo != null) {
            if (StrUtil.isNotBlank(roleVo.getName())) {
                criteria.andLike("name","%".concat(roleVo.getName()).concat("%"));
            }
        }
        PageHelper.startPage(page, rows);
        List<Role> roleList = super.getByExample(example);
        PageInfo roleVoPageInfo = new PageInfo(roleList);
        roleVoPageInfo.setList(setOtherProperties(roleList));
        return roleVoPageInfo;
    }

    /**
     * 根据条件查询所有系统角色信息对象列表
     *
     * @param roleVo
     * @return List<Role>
     */
    @Override
    //@Cacheable
    public List<RoleVo> getAllRole(RoleVo roleVo){
        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        List<RoleVo> roleVoList = setOtherProperties(super.getByExample(example));
        return roleVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param role
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public RoleVo setOtherProperties(Role role){
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(role,roleVo);
        return roleVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param roles
     * @return List<RoleVo>
    */
    @Override
    //@Cacheable
    public List<RoleVo> setOtherProperties(List<Role> roles) {
        List<RoleVo> roleVos = new ArrayList<>();
        List<UserRole> userRoles = userRoleMapper.selectAll();
        List<RoleMenu> roleMenus = roleMenuMapper.selectAll();
        if (CollectionUtil.isNotEmpty(roles)) {
            for (Role role : roles) {
                RoleVo roleVo = new RoleVo();
                BeanUtils.copyProperties(role, roleVo);
                int i = 0;
                int j = 0;
                for (RoleMenu roleMenu : roleMenus) {
                    if (ObjectUtil.equal(roleMenu.getRoleId(),roleVo.getId())){
                        i = i+1;
                    }
                }
                for (UserRole userRole : userRoles) {
                    if (ObjectUtil.equal(userRole.getRoleId(),roleVo.getId())){
                        j = j+1;
                    }
                }
                roleVo.setRoleMenuCount(i);
                roleVo.setUserRoleCount(j);
                roleVos.add(roleVo);
            }
        }
        return roleVos;
    }

    /**
     * 根据roleId删除关联用户、菜单信息
     * @param ids
     */
    @Override
    @Transactional
    public void delAllByIds(String[] ids, Role role){
        if (ArrayUtil.isNotEmpty(ids)) {
            Example example = new Example(UserRole.class);
            example.createCriteria().andIn("roleId", Arrays.asList(ids));
            userRoleMapper.deleteByExample(example);
            Example example1 = new Example(RoleMenu.class);
            example1.createCriteria().andIn("roleId", Arrays.asList(ids));
            roleMenuMapper.deleteByExample(example1);
            super.delByIds(Arrays.asList(ids), role,User.class,UserRole.class,RoleMenu.class);
        }
    }

}
